/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.service.CategoryService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.CmsBusiness;
import com.thinkgem.jeesite.modules.cms.service.CmsBusinessService;

import java.util.List;

/**
 * 商家信息录入展示Controller
 * @author 张朋
 * @version 2016-06-16
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/cmsBusiness")
public class CmsBusinessController extends BaseController {

	@Autowired
	private CmsBusinessService cmsBusinessService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private SystemService systemService;

    @Autowired
    private CategoryService categoryService;
	
	@ModelAttribute
	public CmsBusiness get(@RequestParam(required=false) String id) {
		CmsBusiness entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = cmsBusinessService.get(id);
		}
		if (entity == null){
			entity = new CmsBusiness();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:cmsBusiness:view")
	@RequestMapping(value = {"list", ""})
	public String list(CmsBusiness cmsBusiness, HttpServletRequest request, HttpServletResponse response, Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        User user = systemService.getUser(principal.getId());
        if(user.getUserType().equals("4")){
            cmsBusiness.setUser(user);
        }else{
            cmsBusiness.setUser(null);
        }
        Page<CmsBusiness> page=cmsBusinessService.findPage(new Page<CmsBusiness>(request, response), cmsBusiness);
		model.addAttribute("page", page);
		return "modules/cms/cmsBusinessList";
	}

	@RequiresPermissions("cms:cmsBusiness:view")
	@RequestMapping(value = "form")
	public String form(CmsBusiness cmsBusiness, Model model) {
        if (cmsBusiness.getCategory()!=null && StringUtils.isNotBlank(cmsBusiness.getCategory().getId())){
            List<Category> list = categoryService.findByParentId(cmsBusiness.getCategory().getId(), Site.getCurrentSiteId());
            if (list.size() > 0){
                cmsBusiness.setCategory(null);
            }else{
                cmsBusiness.setCategory(categoryService.get(cmsBusiness.getCategory().getId()));
            }
        }
		model.addAttribute("cmsBusiness", cmsBusiness);

		return "modules/cms/cmsBusinessForm";
	}

	@RequiresPermissions("cms:cmsBusiness:edit")
	@RequestMapping(value = "save")
	public String save(CmsBusiness cmsBusiness, Model model, RedirectAttributes redirectAttributes) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        User user = systemService.getUser(principal.getId());
		if (!beanValidator(model, cmsBusiness)){
			return form(cmsBusiness, model);
		}
        cmsBusiness.setUser(user);
//        cmsBusiness.setMobile(user.getMobile());
//        cmsBusiness.setPhone(user.getPhone());
		cmsBusinessService.save(cmsBusiness);
		addMessage(redirectAttributes, "保存操作成功");
        String userId="";
        String categoryId = cmsBusiness.getCategory()!=null?cmsBusiness.getCategory().getId():null;
        if(user.getUserType().equals("4")){
            userId= cmsBusiness.getUser()!=null?cmsBusiness.getUser().getId():null;
        }
		return "redirect:"+Global.getAdminPath()+"/cms/cmsBusiness/?repage&category.id="+ categoryId +"&user.id="+userId;
	}
	
	@RequiresPermissions("cms:cmsBusiness:edit")
	@RequestMapping(value = "delete")
	public String delete(CmsBusiness cmsBusiness, RedirectAttributes redirectAttributes) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        User user = systemService.getUser(principal.getId());
		cmsBusinessService.delete(cmsBusiness);
		addMessage(redirectAttributes, "删除操作成功");
        String userId="";
        String categoryId = cmsBusiness.getCategory()!=null?cmsBusiness.getCategory().getId():null;
        if(user.getUserType().equals("4")){
            userId= cmsBusiness.getUser()!=null?cmsBusiness.getUser().getId():null;
        }
        return "redirect:"+Global.getAdminPath()+"/cms/cmsBusiness/?repage&category.id="+ categoryId +"&user.id="+userId;
	}



}